package com.zxp.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zxp.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000135648432";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC6U122xT2jssaR9s4Gzcr8hDFS0zRSyrBLru2zGLtU4QCCegZTFAZDWoAcDqQmY5vyx1gcVJIwBV/EBfB//GiJg8Bh5R2VSSEnpzCRE/hNj0njx+zWgTb53ztb5zJeKpsMfnYvI2fr+yWyIrHepanrU6d9R68Ft+hg4kF1kWgDgzf9pdPpEyjUQLy8nb25H9/agGt0NT6pcZH+isowbEinAZ+zym+P9qyPuQejnmPTQe/VVJF0Cd0GQy9j7bGchDdozaeZmZnUSLGXURlB/45W1JMWrZb5fOdOUUkcQyJZZJSwX7MHtSr4PhvqHwPMyUGRbtqbhyxDF7+pQVAAdf7xAgMBAAECggEBAJ6uAsB1x+HSjcOfQaSPvFBDMFJOJmBf17YMushsxpUprapwy1QTWKYhu7qpSWicKiKfXE18RKd63Czvtfr5dfmu57c3TXNmvUKs4vKa5DsBTbcb+3pOvwgb8cx53+5MMJF6XaIVcBqLX3YOkBaCVWxhaOGLm+Wt0sw3w/GhLMjXkNZU3UJCBCEMpqB/b2crxhCUav+/7XP2W8sFJ8WG+J3ujmFmR3RUxExoYVB6lPHwiW6uSdIFr3bHTlZ1vWLGmdp/sB45/lr3ynwy7uCuhFbSdWW/Nu7W8CkAK/2Amhx+FKjne7yepFBMmNj2TTOkRsqitC6OfByqGuQjMIxqdAECgYEA717JEmerZhfLzK21Xpba2eLEDucpfs7Pn9qRYLZxvpivnvz1raGWOoUZw+52yYsb167THjKdUxCGBcMM89Vk/MPPINLtak2PRHOTDRhlJ7+YxwdaJXYv+H1n+jOo98BY6Vnz9CieQS8RRqChFcdSu5KuIhrRRCnBWHrpQdDRnMECgYEAx0Ut15zwM2jEnsHrPSEp1P5QyAbcavoRAX7GEaakF5zNW3aQLDCB5fdRZ9/wuZMmKCcOrtcty2pMX7+eLwuNPBRnXz5JBH5crPkwcMnNdKlNPCLTc6Zqz5lVDjWf7K5RaiVwOVGXXYYArjNreNIzBwjQWT1OJ+6Y49RNG7kwfjECgYEAl0QX3siimM663Gp9rfu4624RJbXRFDCPJl6R9kL1GFQWz2rduk7dx7dq89VTPcPp4V+6TGAB6YcMmsGLAt+8hLscvopsu/utaL18lQ7whbDfVA8CWaAZanedaJCS25Wy2iwdLRMqBtk+mppRkZi3YI+t+SFnUyLr6wUpOI1EmUECgYEAnZ4MmqVaIW/XRjPg3CNlMUJPp0g2BK/zz5j1FnUXCk5O6O+pLirYNIwhaeZqRH8uCUiLhqRRZrqX2ox4lIGwnzsMYPulSdahe3hHZo9Y4dQgcVaiprGVfsuryWeFLfLcFveyAsSU07bwxtmJQrIRBplkI5IoRdGmQV9l9m3O+6ECgYBPByOQZspIyC+aVfIXMvdK85aM+8jRPe6lSZ7c0i4N/ShBD+PNd6NZPc7+M5MAo0ExzDMnRIVUMdtYWSGQPvNJCXFVb/skttIpNZvRQlZeb4893nMjO7IXYuTBxVikT6HCcwSKOXs6cgBJ5gzMZZGtOYwoPpq4fxTpSRVyOY/jKQ==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtY3IH5CXYa2NBZGySx3PPXIJ64W9TamqUgcy8uCTzcRJ7I0ru/nuQuh3EF6OshJfepJYrT9WyioeaK6uL7Ad+VyrjjlFP92z3D2rqVJ6CazkmRurAbF0H0Nu06kTWSikFaECSYL5ApfF5tORiGYxxEYqg7SLmXCVmKDVyFWzYgZa2hZdjJugxx4GzynW5RJzyiopVVQrZVK7pHplb9qLH6ZVu++JnW9JlHlMUYjqWDII0CdmIwfSKkAkDy+AEVhz/pe2y+6V/b0Q+rr+YjLL4kOXcbTGoSlT19Qo7cgbMCNl5DzZx95QQ+wgyK99huKAvIYl5HVfiRUy/MCmr07IEwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://979jvp.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

//    收单的时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
